package com.manager.plat.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.manager.plat.dao.IBalanceDao;
import com.manager.plat.dao.IConfigBusinessDao;
import com.manager.plat.dao.IConfigLowerPartnerDao;
import com.manager.plat.dao.IConfigRouterDao;
import com.manager.plat.util.ExcelCreate;
import com.manager.plat.vo.BalanceVo;
import com.manager.plat.vo.ConfigBusinessVo;
import com.manager.plat.vo.ConfigLowerPartnerVo;
import com.manager.plat.vo.TermVo;
import com.opensymphony.xwork2.ActionSupport;

public class BalanceAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = -5582807219307248753L;

	private static Logger logger = Logger.getLogger(BalanceAction.class);
	
	private IBalanceDao balanceDao;						//结算信息数据接口
	private IConfigBusinessDao configBusinessDao;		//网关信息数据接口
	private IConfigLowerPartnerDao configLowerPartnerDao;//渠道信息数据接口
	private IConfigRouterDao configRouterDao;			//路由信息数据接口
	private List<BalanceVo> balanceVoList;				//结算信息VO
	private List<ConfigBusinessVo> configBusinessVoList;//网关信息数据列表
	private List<ConfigLowerPartnerVo> configLowerPartnerVoList;//渠道信息列表
	private List<String> spnumberList;					//长号码信息列表
	private TermVo term;								//查询条件VO
	private String exportName;							//导出表格名称
	
	/**进入通道结算页面*/
	public String getGateway(){
		/**获得网关信息列表*/
		configBusinessVoList = configBusinessDao.getConfigBusinessList();
		term= null;
		balanceVoList = null;
		return SUCCESS;
	}
	/**查询通道结算信息*/
	public String getGatewayBalance(){
		/**获得业务号码*/
		int sp_code_id = term.getSp_code_id();
		/**获得开始时间*/
		String time_begin = term.getTime_begin();
		/**获得结束时间*/
		String time_end = term.getTime_end();
		
		balanceVoList = balanceDao.getGatewayBalance(sp_code_id, time_begin, time_end);
		return SUCCESS;
	}
	public String exportGatewayBalance(){
		/**获得业务号码*/
		int sp_code_id = term.getSp_code_id();
		/**获得开始时间*/
		String time_begin = term.getTime_begin();
		/**获得结束时间*/
		String time_end = term.getTime_end();
		
		balanceVoList = balanceDao.getGatewayBalance(sp_code_id, time_begin, time_end);
		
		ExcelCreate excelCreate = new ExcelCreate();
		excelCreate.createRow(0);
		writeGatewayTitle(excelCreate);
		writeGatewayBill(excelCreate,balanceVoList);
		
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			excelCreate.downloadExcel(excelCreate.getWorkbook(), response,exportName);
		} catch (IOException e) {
			logger.debug("下载通道结算异常：" + ExceptionUtils.getFullStackTrace(e));
		}
		
		return null;
	}
	
	private void writeGatewayTitle(ExcelCreate excelCreate){
		excelCreate.setCellTitle(0, "通道短号码");
		excelCreate.setCellTitle(1, "计费数量");
		excelCreate.setCellTitle(2, "计费类型");
		excelCreate.setCellTitle(3, "计费金额(元)");
		excelCreate.setCellTitle(4, "计费时长(分)");
		excelCreate.setCellTitle(5, "通道收入(元)");
	}
	
	private void writeGatewayBill(ExcelCreate excelCreate,List<BalanceVo> balanceVoList){
		int currentRow = 1;
		if(balanceVoList != null && balanceVoList.size() > 0){
			int fee_minute_count = 0;
			int fee_price_count = 0;
			for(BalanceVo balanceVo : balanceVoList){
				excelCreate.createRow(currentRow);
				
				excelCreate.setCellTitle(0, balanceVo.getSp_code());
				excelCreate.setCellTitle(1, String.valueOf(balanceVo.getFee_user_num()));
				if(balanceVo.getFee_type() == 1){
					excelCreate.setCellTitle(2, "按时计费");
				}else if(balanceVo.getFee_type() == 2){
					excelCreate.setCellTitle(2, "按条计费");
				}
				
				excelCreate.setCellTitle(3, String.valueOf(balanceVo.getFee_code()));
				excelCreate.setCellTitle(4, String.valueOf(balanceVo.getFee_minute()));
				excelCreate.setCellTitle(5, String.valueOf(balanceVo.getFee_price()));
				
				fee_minute_count += balanceVo.getFee_minute();
				fee_price_count += balanceVo.getFee_price();
				currentRow++;
			}
			
			excelCreate.createRow(currentRow);
			excelCreate.setCellTitle(0, "总计");
			excelCreate.setCellTitle(1, "");
			excelCreate.setCellTitle(2, "");
			excelCreate.setCellTitle(3, "");
			excelCreate.setCellTitle(4, String.valueOf(fee_minute_count));
			excelCreate.setCellTitle(5, String.valueOf(fee_price_count));
		}
	}
	
	/**进入业务结算页面*/
	public String getRouter(){
		/**获得渠道信息列表*/
		configLowerPartnerVoList = configLowerPartnerDao.getConfigLowerPartnerList();
		spnumberList = null;
		term = null;
		
		balanceVoList = null;
		return SUCCESS;
	}
	/**查询业务结算信息*/
	public String getRouterBalance(){
		/**获得业务号码*/
		String sp_number = term.getSp_number();
		/**获得渠道信息*/
		int lower_id = term.getLower_id();
		/**获得开始时间*/
		String time_begin = term.getTime_begin();
		/**获得结束时间*/
		String time_end = term.getTime_end();
		/**根据渠道ID获得渠道长号码列表*/
		spnumberList = configRouterDao.getSpnumberPartnerList(lower_id);
		
		balanceVoList = balanceDao.getRouterBalance(sp_number, lower_id, time_begin, time_end);
		
		if(balanceVoList != null && balanceVoList.size() > 0){
			Map<String, BalanceVo> balanceVoListMap = new HashMap<String, BalanceVo>();
			for(BalanceVo balanceVo : balanceVoList){
				/**获得业务编码*/
				String sp_code = balanceVo.getSp_code();
				/**获得是否过滤*/
				int isfilter = balanceVo.getIsfilter();
				if(balanceVoListMap.get(sp_code) != null){
					if(isfilter == 0){
						balanceVoListMap.get(sp_code).setFee_user_num(balanceVo.getFee_user_num());
						balanceVoListMap.get(sp_code).setFee_minute(balanceVo.getFee_minute());
						balanceVoListMap.get(sp_code).setFee_price(balanceVo.getFee_price());
					}else{
						balanceVoListMap.get(sp_code).setFee_user_num_filter(balanceVo.getFee_user_num());
						balanceVoListMap.get(sp_code).setFee_minute_filter(balanceVo.getFee_minute());
						balanceVoListMap.get(sp_code).setFee_price_filter(balanceVo.getFee_price());
					}
				}else{
					if(isfilter == 0){
						balanceVoListMap.put(sp_code, balanceVo);
					}else{
						balanceVo.setFee_user_num(0);
						balanceVo.setFee_user_num_filter(balanceVo.getFee_user_num());
						balanceVo.setFee_minute(0);
						balanceVo.setFee_minute_filter(balanceVo.getFee_minute());
						balanceVo.setFee_price(0);
						balanceVo.setFee_price_filter(balanceVo.getFee_price());
						balanceVoListMap.put(sp_code, balanceVo);
					}
				}
			}
			
			if(balanceVoListMap != null && balanceVoListMap.size() > 0){
				balanceVoList.clear();
				for(BalanceVo balanceVo : balanceVoListMap.values()){
					balanceVoList.add(balanceVo);
				}
			}
			
		}
		return SUCCESS;
	}
	public String exportRouterBalance(){
		/**获得业务号码*/
		String sp_number = term.getSp_number();
		/**获得渠道信息*/
		int lower_id = term.getLower_id();
		/**获得开始时间*/
		String time_begin = term.getTime_begin();
		/**获得结束时间*/
		String time_end = term.getTime_end();
		/**根据渠道ID获得渠道长号码列表*/
		spnumberList = configRouterDao.getSpnumberPartnerList(lower_id);
		
		balanceVoList = balanceDao.getRouterBalance(sp_number, lower_id, time_begin, time_end);
		
		if(balanceVoList != null && balanceVoList.size() > 0){
			Map<String, BalanceVo> balanceVoListMap = new HashMap<String, BalanceVo>();
			for(BalanceVo balanceVo : balanceVoList){
				/**获得业务编码*/
				String sp_code = balanceVo.getSp_code();
				/**获得是否过滤*/
				int isfilter = balanceVo.getIsfilter();
				if(balanceVoListMap.get(sp_code) != null){
					if(isfilter == 0){
						balanceVoListMap.get(sp_code).setFee_user_num(balanceVo.getFee_user_num());
						balanceVoListMap.get(sp_code).setFee_minute(balanceVo.getFee_minute());
						balanceVoListMap.get(sp_code).setFee_price(balanceVo.getFee_price());
					}else{
						balanceVoListMap.get(sp_code).setFee_user_num_filter(balanceVo.getFee_user_num());
						balanceVoListMap.get(sp_code).setFee_minute_filter(balanceVo.getFee_minute());
						balanceVoListMap.get(sp_code).setFee_price_filter(balanceVo.getFee_price());
					}
				}else{
					if(isfilter == 0){
						balanceVoListMap.put(sp_code, balanceVo);
					}else{
						balanceVo.setFee_user_num(0);
						balanceVo.setFee_user_num_filter(balanceVo.getFee_user_num());
						balanceVo.setFee_minute(0);
						balanceVo.setFee_minute_filter(balanceVo.getFee_minute());
						balanceVo.setFee_price(0);
						balanceVo.setFee_price_filter(balanceVo.getFee_price());
						balanceVoListMap.put(sp_code, balanceVo);
					}
				}
			}
			
			if(balanceVoListMap != null && balanceVoListMap.size() > 0){
				balanceVoList.clear();
				for(BalanceVo balanceVo : balanceVoListMap.values()){
					balanceVoList.add(balanceVo);
				}
			}
			
		}
		
		ExcelCreate excelCreate = new ExcelCreate();
		excelCreate.createRow(0);
		writeRouterTitle(excelCreate);
		writeRouterBill(excelCreate,balanceVoList);
		
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			excelCreate.downloadExcel(excelCreate.getWorkbook(), response,exportName);
		} catch (IOException e) {
			logger.debug("下载业务结算异常：" + ExceptionUtils.getFullStackTrace(e));
		}
		
		return null;
	}
	
	private void writeRouterTitle(ExcelCreate excelCreate){
		excelCreate.setCellTitle(0, "渠道名称");
		excelCreate.setCellTitle(1, "业务长号码");
		excelCreate.setCellTitle(2, "同步数量");
		excelCreate.setCellTitle(3, "过滤数量");
		excelCreate.setCellTitle(4, "计费类型");
		excelCreate.setCellTitle(5, "计费金额(元)");
		excelCreate.setCellTitle(6, "同步计费时长(分)");
		excelCreate.setCellTitle(7, "过滤计费时长(分)");
		excelCreate.setCellTitle(8, "同步收入(元)");
		excelCreate.setCellTitle(9, "过滤收入(元)");
		excelCreate.setCellTitle(10, "总收入(元)");
	}
	
	private void writeRouterBill(ExcelCreate excelCreate,List<BalanceVo> balanceVoList){
		int currentRow = 1;
		if(balanceVoList != null && balanceVoList.size() > 0){
			int fee_minute_count = 0;
			int fee_minute_filter_count = 0;
			int fee_price_count = 0;
			int fee_price_filter_count = 0;
			for(BalanceVo balanceVo : balanceVoList){
				excelCreate.createRow(currentRow);
				
				excelCreate.setCellTitle(0, balanceVo.getLower_name());
				excelCreate.setCellTitle(1, balanceVo.getSp_code());
				
				excelCreate.setCellTitle(2, String.valueOf(balanceVo.getFee_user_num()));
				excelCreate.setCellTitle(3, String.valueOf(balanceVo.getFee_user_num_filter()));
				if(balanceVo.getFee_type() == 1){
					excelCreate.setCellTitle(4, "按时计费");
				}else if(balanceVo.getFee_type() == 2){
					excelCreate.setCellTitle(4, "按条计费");
				}
				
				excelCreate.setCellTitle(5, String.valueOf(balanceVo.getFee_code()));
				excelCreate.setCellTitle(6, String.valueOf(balanceVo.getFee_minute()));
				excelCreate.setCellTitle(7, String.valueOf(balanceVo.getFee_minute_filter()));
				excelCreate.setCellTitle(8, String.valueOf(balanceVo.getFee_price()));
				excelCreate.setCellTitle(9, String.valueOf(balanceVo.getFee_price_filter()));
				excelCreate.setCellTitle(10, String.valueOf(balanceVo.getFee_price() + balanceVo.getFee_price_filter()));
				
				fee_minute_count += balanceVo.getFee_minute();
				fee_minute_filter_count += balanceVo.getFee_minute_filter();
				fee_price_count += balanceVo.getFee_price();
				fee_price_filter_count += balanceVo.getFee_price_filter();
				currentRow++;
			}
			
			excelCreate.createRow(currentRow);
			excelCreate.setCellTitle(0, "总计");
			excelCreate.setCellTitle(1, "");
			excelCreate.setCellTitle(2, "");
			excelCreate.setCellTitle(3, "");
			excelCreate.setCellTitle(4, "");
			excelCreate.setCellTitle(5, "");
			excelCreate.setCellTitle(6, String.valueOf(fee_minute_count));
			excelCreate.setCellTitle(7, String.valueOf(fee_minute_filter_count));
			excelCreate.setCellTitle(8, String.valueOf(fee_price_count));
			excelCreate.setCellTitle(9, String.valueOf(fee_price_filter_count));
			excelCreate.setCellTitle(10, String.valueOf(fee_price_count + fee_price_filter_count));
		}
	}
	
	
	public IBalanceDao getBalanceDao() {
		return balanceDao;
	}
	public void setBalanceDao(IBalanceDao balanceDao) {
		this.balanceDao = balanceDao;
	}
	public IConfigBusinessDao getConfigBusinessDao() {
		return configBusinessDao;
	}
	public void setConfigBusinessDao(IConfigBusinessDao configBusinessDao) {
		this.configBusinessDao = configBusinessDao;
	}
	public IConfigLowerPartnerDao getConfigLowerPartnerDao() {
		return configLowerPartnerDao;
	}
	public void setConfigLowerPartnerDao(IConfigLowerPartnerDao configLowerPartnerDao) {
		this.configLowerPartnerDao = configLowerPartnerDao;
	}
	public IConfigRouterDao getConfigRouterDao() {
		return configRouterDao;
	}
	public void setConfigRouterDao(IConfigRouterDao configRouterDao) {
		this.configRouterDao = configRouterDao;
	}
	public List<ConfigBusinessVo> getConfigBusinessVoList() {
		return configBusinessVoList;
	}
	public void setConfigBusinessVoList(List<ConfigBusinessVo> configBusinessVoList) {
		this.configBusinessVoList = configBusinessVoList;
	}
	public List<ConfigLowerPartnerVo> getConfigLowerPartnerVoList() {
		return configLowerPartnerVoList;
	}
	public void setConfigLowerPartnerVoList(List<ConfigLowerPartnerVo> configLowerPartnerVoList) {
		this.configLowerPartnerVoList = configLowerPartnerVoList;
	}
	public List<String> getSpnumberList() {
		return spnumberList;
	}
	public void setSpnumberList(List<String> spnumberList) {
		this.spnumberList = spnumberList;
	}
	public TermVo getTerm() {
		return term;
	}
	public void setTerm(TermVo term) {
		this.term = term;
	}
	public List<BalanceVo> getBalanceVoList() {
		return balanceVoList;
	}
	public void setBalanceVoList(List<BalanceVo> balanceVoList) {
		this.balanceVoList = balanceVoList;
	}
	public String getExportName() {
		return exportName;
	}
	public void setExportName(String exportName) {
		this.exportName = exportName;
	}
}
